import {observable, action} from "mobx";
import request from "../utils/request";
import * as api from "../api/api";
import {LoadHoc, loading} from '../hoc/loadHoc';

export default class HomeStore extends LoadHoc {
  /**
   * 首页数据
   */
  @observable home: api.YNJWTHomeVOIndexVO = {};

  /**
   * 获取首页数据
   */
  @loading @action
  async getHomeInfo(params: {
    callback?: (home: api.YNJWTHomeVOIndexVO) => void;
  }) {
    const {callback} = params;
    const home = await request(api.ApphomeApiFetchParamCreactor.apphomeGetHomeInfoAsync({}));
    this.home = home;
    callback && callback(home);
  }

  /**
   * 保存发布信息
   */
  @loading @action
  async saveUserInfomations(params: {
    form: api.YNJWTHomeFormInfomationForm,
    callBack: () => void
  }) {
    const {form, callBack} = params
    await request(api.ApphomeApiFetchParamCreactor.apphomeSaveUserInfomationsAsync({
        form
      }
    ))
    callBack && callBack()
  }

  /**
  * 获取分页最新信息
  */
  @observable allInfomations: api.YNJWTHomeVOInfomationMiniVO = {};
  @loading @action
  async getAllInfomations(params: {
    formPublicationType: number,
    pageIndex: number,
    pageSize: number,
    callback?: (home: api.YNJWTHomeVOInfomationMiniVO) => void;
  }) {
    const {formPublicationType,pageIndex,pageSize,callback} = params;
    const allInfomations = await request(api.ApphomeApiFetchParamCreactor.apphomeGetAllInfomationsAsync({
      formPublicationType,
      formSkipCount:  (pageIndex-1)*pageSize,
      formMaxResultCount:pageSize
    }));
    this.allInfomations = allInfomations;
    callback && callback(allInfomations);
  }

  /**
   * 获取信息详情
   */
  @observable infomationInfos: api.YNJWTInfomationsVOInfomationVO = {};
  @loading @action
  async getInfomationInfos(params: {
    formId,
    callback?: (home: api.YNJWTInfomationsVOInfomationVO) => void;
  }) {
    const {formId,callback} = params;
    const infomationInfos = await request(api.ApphomeApiFetchParamCreactor.apphomeGetInfomationInfosAsync({
      formId
    }));
    this.infomationInfos = infomationInfos;
    callback && callback(infomationInfos);
  }
}
